#include "stdafx.h"

using namespace DuiLib;

class CBrowserFrameWnd : public CWindowWnd, public INotifyUI, public IDialogBuilderCallback
{
public:
	CBrowserFrameWnd() { }
	LPCTSTR GetWindowClassName() const { return _T("UIMainFrame"); }
	UINT GetClassStyle() const { return CS_DBLCLKS; }
	void OnFinalMessage(HWND /*hWnd*/) { delete this; }

	CControlUI* CreateControl(LPCTSTR pstrClass)
	{
		return NULL;
	}

	void Init()
	{
		m_pCloseBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("closebtn")));
		m_pMaxBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("maxbtn")));
		m_pRestoreBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("restorebtn")));
		m_pMinBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("minbtn")));

		CActiveXUI* pActiveXUI = static_cast<CActiveXUI*>(m_pm.FindControl(_T("ie")));
		if (pActiveXUI)
		{
			
			IWebBrowser2* pWebBrowser = NULL;
			pActiveXUI->GetControl(IID_IWebBrowser2, (void**)&pWebBrowser);
			if (pWebBrowser != NULL)
			{
				pWebBrowser->Navigate2(L"http://lk.fuwu101.com", NULL, NULL, NULL, NULL);
				pWebBrowser->Release();
			}
		}
	}

	void Notify(TNotifyUI& msg)
	{
		if (msg.sType == _T("click"))
		{
			if (msg.pSender == m_pCloseBtn)
				PostQuitMessage(0);
			else if (msg.pSender == m_pMinBtn)			
				SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
			else if (msg.pSender == m_pMaxBtn)			
				SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);
			else if (msg.pSender == m_pRestoreBtn)			
				SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0);

			return;
		}
	}

	LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
		styleValue &= ~WS_CAPTION;
		::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
// 
// 		RECT rcClient;
// 		::GetClientRect(*this, &rcClient);
// 		::SetWindowPos(*this, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, \
// 			rcClient.bottom - rcClient.top, SWP_FRAMECHANGED);

		m_pm.Init(m_hWnd);

		CDialogBuilder builder;
		CControlUI* proot = builder.Create(_T("browser.xml"), (UINT)0, this, &m_pm);
		ASSERT(proot && "Failed to parse XML");
		m_pm.AttachDialog(proot);
		m_pm.AddNotifier(this);

		Init();

		return 0;
	}

	LRESULT OnGetMinMaxInfo(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		MONITORINFO monitor = {};
		monitor.cbSize = sizeof(monitor);
		::GetMonitorInfo(::MonitorFromWindow(*this, MONITOR_DEFAULTTOPRIMARY), &monitor);
		CDuiRect rcWork = monitor.rcWork;
		LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam;
		lpMMI->ptMaxPosition.x = rcWork.left;
		lpMMI->ptMaxPosition.y = rcWork.top;
		lpMMI->ptMaxSize.x = rcWork.right - rcWork.left;
		lpMMI->ptMaxSize.y = rcWork.bottom - rcWork.top;
		bHandled = FALSE;
		return 0;
	}

	LRESULT OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		if( wParam == SC_CLOSE ) {
			::PostQuitMessage(0L);
			bHandled = TRUE;
			return 0;
		}
		BOOL bZoomed = ::IsZoomed(*this);
		LRESULT lRes = CWindowWnd::HandleMessage(uMsg, wParam, lParam);
		if( ::IsZoomed(*this) != bZoomed )
		{
			if( !bZoomed )
			{
				if( m_pMaxBtn ) m_pMaxBtn->SetVisible(false);
				if( m_pRestoreBtn ) m_pRestoreBtn->SetVisible(true);
			}
			else {
				if( m_pMaxBtn ) m_pMaxBtn->SetVisible(true);
				if( m_pRestoreBtn ) m_pRestoreBtn->SetVisible(false);
			}
		}
		return lRes;
	}

	LRESULT OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		POINT pt; pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam);
		::ScreenToClient(*this, &pt);

		RECT rcClient;
		::GetClientRect(*this, &rcClient);

		if( !::IsZoomed(*this) ) {
			RECT rcSizeBox = m_pm.GetSizeBox();
			if( pt.y < rcClient.top + rcSizeBox.top ) {
				if( pt.x < rcClient.left + rcSizeBox.left ) return HTTOPLEFT;
				if( pt.x > rcClient.right - rcSizeBox.right ) return HTTOPRIGHT;
				return HTTOP;
			}
			else if( pt.y > rcClient.bottom - rcSizeBox.bottom ) {
				if( pt.x < rcClient.left + rcSizeBox.left ) return HTBOTTOMLEFT;
				if( pt.x > rcClient.right - rcSizeBox.right ) return HTBOTTOMRIGHT;
				return HTBOTTOM;
			}
			if( pt.x < rcClient.left + rcSizeBox.left ) return HTLEFT;
			if( pt.x > rcClient.right - rcSizeBox.right ) return HTRIGHT;
		}

		CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(pt));
		// 			TCHAR str[MAX_PATH];
		// 			_stprintf_s(str, MAX_PATH - 1, _T("MainFrame::OnNcHitTest GetClass %s\n"), pControl->GetClass());
		// 			OutputDebugString(str);
		if (pControl && _tcsicmp(pControl->GetClass(), _T("LayoutUI")) > 0)
			return HTCAPTION;

		return HTCLIENT;
	}

	LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
	{
		LRESULT lres = 0;
		BOOL bHandled = TRUE;
		switch (uMsg)
		{
		case WM_CREATE:			lres = OnCreate(uMsg, wParam, lParam, bHandled); break;
		case WM_GETMINMAXINFO:	lres = OnGetMinMaxInfo(uMsg, wParam, lParam, bHandled); break;
		case WM_SYSCOMMAND:		lres = OnSysCommand(uMsg, wParam, lParam, bHandled); break;
		default:
			bHandled = FALSE;
		}
		if (bHandled) return lres;

		if (m_pm.MessageHandler(uMsg, wParam, lParam, lres))
			return lres;

		return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
	}

public:
	CPaintManagerUI m_pm;

private:
	CButtonUI* m_pCloseBtn;
	CButtonUI* m_pMaxBtn;
	CButtonUI* m_pRestoreBtn;
	CButtonUI* m_pMinBtn;
};

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
	CPaintManagerUI::SetInstance(hInstance);
	CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath() + _T("skin"));

	HRESULT hr = ::CoInitialize(NULL);
	if (FAILED(hr)) return 0;

	CBrowserFrameWnd* pframe = new CBrowserFrameWnd();
	if (pframe == NULL) return 0;

	pframe->Create(NULL, _T(""), UI_WNDSTYLE_FRAME, WS_EX_STATICEDGE | WS_EX_APPWINDOW, 0, 0, 1024, 738);
	pframe->CenterWindow();
	::ShowWindow(*pframe, SW_SHOWMAXIMIZED);

	CPaintManagerUI::MessageLoop();

	::CoUninitialize();
	return 0;
}